#include<vector>
#include<string>
#include <stack>
#include<memory>
// This is the interface that allows for creating nested lists.
 // You should not implement it, or speculate about its implementation
class NestedInteger {
public:
    // Constructor initializes an empty nested list.
    NestedInteger();

    // Constructor initializes a single integer.
    NestedInteger(int value);

    // Return true if this NestedInteger holds a single integer, rather than a nested list.
    bool isInteger() const;

    // Return the single integer that this NestedInteger holds, if it holds a single integer
    // The result is undefined if this NestedInteger holds a nested list
    int getInteger() const;

    // Set this NestedInteger to hold a single integer.
    void setInteger(int value);

    // Set this NestedInteger to hold a nested list and adds a nested integer to it.
    void add(const NestedInteger& ni);

    // Return the nested list that this NestedInteger holds, if it holds a nested list
    // The result is undefined if this NestedInteger holds a single integer
    const std::vector<NestedInteger>& getList() const;
};

class Solution {
private:
    int pos = 0;
    NestedInteger parse(const std::string& s) {
        NestedInteger res;
        if (pos >= s.length())
            return res;
        if (s[pos] == '[') {
            while (s[pos] != ']') {
                ++pos;
                if (pos >= s.length() || s[pos] == ']')
                    break;
                res.add(parse(s));
            }
            ++pos;
        }
        else {
            int num = 0;
            int positve = s[pos] == '-' ? -1 : 1;
            if (s[pos] == '-')
                ++pos;
            while (pos < s.length()&&s[pos]>='0'&&s[pos]<='9') {
                    num = num * 10 + s[pos] - '0';
                ++pos;
            }
            res.setInteger(positve * num);
        }
        return res;
    }

public:
    NestedInteger deserialize(std::string s) {
        return parse(s);
    }


    //NestedInteger deserialize(std::string s) {
    //    std::stack<std::shared_ptr<NestedInteger>> stack_;
    //    stack_.push(std::make_shared<NestedInteger>());
    //    std::string num = "";
    //    for (char c : s) {
    //        if (c == '[')
    //            stack_.push(std::make_shared<NestedInteger>());
    //        else if (c == ']') {
    //            if (num != "") {
    //                stack_.top()->add(NestedInteger(atoi(num.c_str())));
    //                num = "";
    //            }
    //            auto tmp = stack_.top();
    //            stack_.pop();
    //            stack_.top()->add(*tmp);
    //        }
    //        else if (c == '-' || (c >= '0' && c <= '9'))
    //            num += c;
    //        else if (c == ',') {
    //            if (num != "") {
    //                stack_.top()->add(NestedInteger(atoi(num.c_str())));
    //                num = "";
    //            }
    //        }
    //    }
    //    if (num != "") {
    //        stack_.top()->add(NestedInteger(atoi(num.c_str())));
    //        num = "";
    //    }
    //    
    //    if (stack_.top()->getList().size() > 0) {
    //        auto tmp = stack_.top()->getList().at(0);
    //        return tmp;
    //    }
    //    return NestedInteger();
    //}
};